var Promise = require('./es6-promise.min');
var formatDate = require('./formatDate.js');
/**
 * 时间选择
 */

/**
   * 当前时间加+n毫秒
   * @param curDate 当前时间
   * @param ms      需要增加的毫秒数
   * @returns {String} 格式化后的时间
   */
const getCurrentDate = function (curDate, ms) {
  if (typeof curDate == "string") curDate = curDate.replace(/-/g, "/");
  return formatDate(new Date(curDate).getTime() + (ms || 0), 'yyyy-MM-dd hh:mm:ss');
};
const now = new Date();
const oneDay = 24 * 60 * 60 * 1000;
const dataTodayStart = formatDate(now, 'yyyy-MM-dd 00:00:00');
const dataTodayEnd = getCurrentDate(dataTodayStart, 24 * 60 * 60 * 1000 - 1);
const nowDayOfWeek = now.getDay(); //今天本周的第几天
const nowMonth = now.getMonth() + 1; //当前月
const nowDay = now.getDate(); //当前日
const lastMouthEnd = getCurrentDate(dataTodayEnd, -(nowDay) * oneDay);
const lastMouthStart = getCurrentDate(dataTodayStart, -(nowDay + new Date(lastMouthEnd).getDate() - 1) * oneDay);


const dateList = '今天 昨天 最近三天 最近七天 本月 上个月'.split(' ');


const dateToParms = [
  {
    start_time: dataTodayStart
  },
  {
    start_time: getCurrentDate(dataTodayStart, -1 * oneDay),
    end_time: getCurrentDate(dataTodayEnd, -1 * oneDay)
  },
  {
    start_time: getCurrentDate(dataTodayStart, -3 * oneDay)
  },
  {
    start_time: getCurrentDate(dataTodayStart, -6 * oneDay)
  },
  {
    start_time: getCurrentDate(dataTodayStart, -(nowDay - 1) * oneDay)
  },
  {
    start_time: lastMouthStart,
    end_time: lastMouthEnd
  },
];

var timeChoose =  () => {
  return new Promise((resolve, reject) => {
    wx.showActionSheet({
      itemList: dateList,
      success(res) {
        if (res.cancel) {
          return reject(res)
        }
        resolve({
          tapIndex: res.tapIndex,
          time_cn: dateList[res.tapIndex],
          time_parms: dateToParms[res.tapIndex]
        })

      },
      fail: reject
    })
  })

}

timeChoose.dateToParms = dateToParms;
timeChoose.dateList = dateList;

module.exports = timeChoose